/**
 * @Title: ActivityManager.java
 * @Package boco.common.core
 * @Description: TODO
 * @author li_xiaoyue
 * @version 2013-5-13
 */
package com.hktf.corelib.core;

import android.app.Activity;

import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.LogMds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;

/**
 * activityManager
 *
 * @author li_xiaoyue
 * @date 2013-5-13 下午3:48:27
 */
public class ActivityManager {
    private Map<String, Activity> activitys = new ConcurrentHashMap<String, Activity>();// 存储对应的路净值和activity的引用
    private static Stack<Activity> activityStack;
    private static ActivityManager instance;
    private static HashMap<String, String> menuidtoActivity = new HashMap<>();

    public static HashMap<String, String> getMenuidtoActivity() {
        return menuidtoActivity;
    }


    public static void putMenu(String menuid, String ActPath) {
        menuidtoActivity.put(menuid, ActPath);
    }

    public static void removeMenu(String menuid) {
        menuidtoActivity.remove(menuid);
    }

    public static String getActStringbyMenuID(String menuid) {
        return menuidtoActivity.get(menuid);
    }

    public Map<String, Activity> getActivitys() {
        return activitys;
    }
    public Activity findActivity(String className){
        for (int i = activityStack.size() -1; i > -1; i--) {
            Activity activity = activityStack.get(i);
            if (activity.getComponentName().getClassName().contains(className)) {
                return activity;
            }
        }
        return null;
    }

    public void setActivitys(Map<String, Activity> activitys) {
        this.activitys = activitys;
    }

    private ActivityManager() {
    }

    public static ActivityManager getScreenManager() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }


    public void popActivityExceptAtsmain() {
        Class<?> className = null;
        String packageName = CommUtils.getPackageName(CommUtils.getContext());
        try {
            className = Class.forName(packageName + ".ATSMainActivity");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<String> classList = new ArrayList<String>();
        classList.add(className.toString());
        popAllActivityExceptOne(classList);
    }

    /**
     * 退出栈顶Activity
     *
     * @param activity
     * @return void
     * @throws
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
            if (!activityStack.contains(activity)) {
                return;
            }
            // 在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
            LogMds.e("yubl_activity", "pop:" + activity);
            activity.finish();
            activityStack.remove(activity);
            Set<Entry<String, Activity>> set = activitys.entrySet();
            Iterator<Entry<String, Activity>> iterator = set.iterator();
            while (iterator.hasNext()) {
                Entry<String, Activity> entry = iterator.next();
                if (entry.getValue() == activity) {
                    iterator.remove();
                }
            }

            activity = null;
        }
    }

    /**
     * 获得当前栈顶Activity
     *
     * @return
     * @return Activity
     * @throws
     */
    public Activity currentActivity() {
        Activity activity = null;
        if (!activityStack.empty())
            activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 将当前Activity推入栈中
     *
     * @param activity
     * @return void
     * @throws
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        LogMds.e("yubl_activity", "push:" + activity);

        activityStack.add(activity);
    }

    /**
     * @param <T>
     *            退出栈中所有Activity
     * @param cls
     * @return void
     * @throws
     */
    public <T> void popAllActivityExceptOne(List<String> list) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            boolean isFind = false;
            for (int i = 0; i < list.size(); i++) {
                if (activity.getClass().toString()
                        .equalsIgnoreCase(list.get(i))) {
                    isFind = true;
                    break;
                }
            }
            if (isFind) {
                break;
            }

            popActivity(activity);
        }
    }
    /**
     * @param <T>
     *            退出栈中所有Activity
     * @param cls
     * @return void
     * @throws
     */
    public <T> void popAllActivityExcept(List<String> list) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            boolean isFind = false;
            for (int i = 0; i < list.size(); i++) {
                if (activity.getClass().toString()
                        .contains(list.get(i))) {
                    isFind = true;
                    break;
                }
            }
            if (isFind) {
                break;
            }

            popActivity(activity);
        }
    }

    /**
     * @param <T>
     *            退出栈中所有Activity
     * @param cls
     * @return void
     * @throws
     */
    public <T> void popAllActivityExceptOne(Activity activity) {
        while (activity != currentActivity()) {
            popActivity(currentActivity());
        }

    }

    /**
     * 检查栈中是否存在此activity
     */
    public boolean isContainActivityExceptTop(Activity activity) {
        boolean isContain = false;
        List<Activity> list = activityStack
                .subList(0, activityStack.size() - 1);
        for (Activity stack : list) {
            if (stack.getClass().getName()
                    .equalsIgnoreCase(activity.getClass().getName())) {
                isContain = true;
                break;
            }
        }

        return isContain;
    }


    public void popActivityExceptTop() {
        if (activityStack == null||activityStack.size()==0) {
            return;
        }
        Activity activity = activityStack.get(0);
        if (activity != null) {
            popAllActivityExceptOne(activity);
        }


    }


    public void popAllActivity() {
        Activity activity = activityStack.get(0);
        if (activity != null) {
            popActivity(activity);
        }
    }

    //hexunwu 关闭所有的activity
    public void popAllActivities() {
        for (int i = activityStack.size() -1; i > -1; i--) {
            Activity activity = activityStack.get(i);
            if (activity != null) {
                popActivity(activity);
            }
        }
    }
}